<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Login
 *
 * @author hassanali
 */
class options extends admin_controller {

    //put your code here
    public function __construct() {
        parent::__construct();
        $this->load->model('admin/options_model', 'options');
    }

    public function index() {
        if ($this->input->post()) {
            $options = $this->input->post('options');
            $options_type = $this->input->post('options_type');
            foreach ($options_type as $key => $value) {
                if($value == 'checkbox'){
                    if(array_key_exists($key, $options))
                            $mvalue = '1';
                    else
                        $mvalue = '0';
                }else{
                    $mvalue = $options[$key];
                }
                $this->options->save(array('value' => $mvalue), array('code' => $key), TRUE);
            }
            $this->themes->set_message(array('url' => current_url(), 'type' => 'success', 'message' => 'message_save_success'));
        }
        $this->pass['options'] = $this->options->get();
        $this->themes->render('options/form', $this->pass);
    }

    public function login() {
        if ($this->input->post()) {
            $this->form_validation->set_rules('username', lang('label_username'), 'trim|required|is_found[user.username]');
            $this->form_validation->set_rules('password', lang('label_password'), 'trim|required|min_length[6]');

            if ($this->form_validation->run()) {

                $username = $this->input->post('username');
                $password = $this->input->post('password');
                $this->user_data = $this->admin->login($username, $password);
                if ($this->user_data) {
                    $contant = array('username' => $username, 'password' => $password, 'ip' => $this->input->ip_address(), 'browser' => $this->input->agent(), 'login' => 1, 'mode' => $this->user_data->mode);
                    $this->admin->add_manager_report(array('action' => 'user-login', 'user_id' => $this->user_data->id, 'contant' => $contant));
                    $this->login_done('admin', ADMIN_FOLDER . '/dashboard');
                } else {
                    $contant = array('username' => $username, 'password' => $password, 'login' => 0, 'ip' => $this->input->ip_address(), 'browser' => $this->input->agent());
                    $this->admin->add_manager_report(array('action' => 'user-login', 'contant' => $contant));
                    $this->themes->set_message(array('url' => 'user/login', 'type' => 'error', 'message' => 'message_login_faild'));
                }
            } else {
                get_validation_errors();
            }
        }

        $this->load->view('admin/login');
    }

    public function logout() {

        if (empty($this->admin_id)) {
            redirect(ADMIN_FOLDER . '/dashboard/login');
        } else {
            $contant = array('username' => $this->session->userdata('admin_NAME'), 'ip' => $this->input->ip_address(), 'browser' => $this->input->agent());
            $this->admin->add_manager_report(array('action' => 'user-logout', 'contant' => $contant));
            $this->destroy_session('admin');
        }
    }

    function my_data() {
        $this->user_form($this->session->userdata('admin_ID'), 'admin', 'dashboard/my_data', 'mydata');
    }

}